home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / Drtf / DRichHandler.cpp < prev    next >
Text File  |  1996-07-05  |  5KB  |  217 lines

  1. // DPICTHandler.cpp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "Dvibrant.h"
  6. #include "DFile.h"
  7. #include "DRichHandler.h"
  8. #include "DRichViewNu.h"
  9. #include "DRichProcess.h"
  10.  
  11.  
  12.  
  13.  
  14. //class  DRichHandler
  15.  
  16. enum {
  17.     kNoData = -1
  18.     };
  19.             
  20.  
  21. DRichHandler::DRichHandler(DRichView* itsDoc, DFile* savefile) :
  22.     fDoc(itsDoc), fDocFile(savefile),
  23.     fProcessor(NULL), fMinDataToProcess(1), fLastScanto( 0),
  24.     fProcessStage(kAtStart), fTasknum(0)
  25. {
  26.     fFormatName = "Text";
  27. }
  28.  
  29. DRichHandler::~DRichHandler()
  30. {
  31.     if (fProcessor) delete fProcessor;
  32. }
  33.  
  34.     
  35. char* DRichHandler::IsRich(char* buf, ulong buflen)
  36. {
  37. #if 0
  38.     char *cp, *cend = buf + MIN(buflen-1, 500);
  39.     for (cp= buf; cp<cend; cp++) {
  40.         //if (*cp < ' ' && *cp != '\n' && *cp != '\r' && *cp != '\t') 
  41.         if (! (*cp >= ' ' || isspace(*cp)) )  
  42.             return NULL;
  43.         }
  44. #endif
  45.     return buf; // ?? assume plain text? or test for garbage (nulls, etc)
  46. }
  47.  
  48. long DRichHandler::IsRichFile( DFile* theFile)
  49. {
  50.     long    startat = kNoData; // no data
  51.     
  52.     theFile->Open();
  53.     if (theFile->IsOpen()) {
  54.         char buf[1024];
  55.         ulong count= 1024; 
  56.  
  57.         theFile->ReadData(buf, count);
  58.         char* cp= IsRich( buf, count);
  59.         if (cp) startat= cp - buf;
  60.         theFile->Close();
  61.         }
  62.     return startat;
  63. }
  64.  
  65. long DRichHandler::IsRichFile( char* filename)
  66. {
  67.     DFile afile(filename, "r");
  68.     return IsRichFile( &afile);
  69. }
  70.  
  71. Boolean DRichHandler::ProcessData( char* cbeg, char* cend, Boolean endOfData, 
  72.                                         ulong& dataRemaining) 
  73. {
  74.     char        * cp;
  75.     //char csave = 0;  
  76.     //ulong     clen;
  77.  
  78.     fcsave= 0;    
  79.     fclen= cend - cbeg; // ?? +1
  80.     cp= cend;
  81.     if (!endOfData) {
  82.         if ((fclen==0) || ( fProcessStage == kAtStart && fclen<fMinDataToProcess)) 
  83.             return false;
  84.         // scan for last newline and stop there !!
  85.         while (cp - cbeg > fLastScanto && !(*cp == '\n' || *cp == '\r')) cp--;
  86.         }
  87.         
  88.     if (cp - cbeg <= fLastScanto) {
  89.         fLastScanto= cend - cbeg;  
  90.         if (!endOfData) return false; // !??! don't process line w/o end
  91.         else cp= cend; 
  92.         }
  93.     fclen= cp - cbeg; // ?? +1
  94.             
  95.     if (fclen && fDocFile) fDocFile->WriteData( cbeg, fclen);
  96.  
  97.     if (!fProcessor) { // this may be bad 
  98.         cp= IsRich(cbeg, fclen);
  99.         if (!cp) return false;
  100.         cbeg= cp;
  101.         fclen= cend - cbeg; //?? +1
  102.         }
  103.         
  104.     return this->ProcessData2( cp, cbeg, cend, endOfData, dataRemaining);
  105. }
  106.  
  107. Boolean DRichHandler::ProcessData2( char* cp, char* cbeg, char* cend, Boolean endOfData, 
  108.                                         ulong& dataRemaining) 
  109. {
  110.  
  111.     if (!fProcessor) {  
  112.         fDoc->SetAutoAdjust(false);
  113.         //fProcessor= this->NewProcessor(NULL,NULL); 
  114.         fProcessor= this->NewProcessor((DFile*)NULL,(Nlm_MonitorPtr)NULL); 
  115.         //FailNIL( fProcessor); //??
  116.         fProcessStage = kInBody;
  117.         }
  118.         
  119.   if (fclen) {
  120.       fProcessor->SetBuffer(cbeg, fclen, endOfData);
  121.         while (fProcessor->GetToken() != DRichprocess::tokEOF) 
  122.             fProcessor->RouteToken();
  123.         }
  124.         
  125.     if (endOfData) { 
  126.         fProcessor->Close(); 
  127.         if (fProcessor->GetTitle()) 
  128.             fDoc->GetWindow()->SetTitle( (char*)fProcessor->GetTitle());
  129.         delete fProcessor; 
  130.         fProcessor= NULL; 
  131.         } 
  132.  
  133.     fDoc->AdjustScroll();
  134.     if (endOfData) fDoc->SetAutoAdjust(true);   
  135.             
  136.     if (cp > cbeg) { // (fcsave) {
  137.         if (fcsave) *cp= fcsave;   
  138.         if (cp > cend) fclen= 0; 
  139.         else fclen= cend - cp; // + 1;
  140.         Nlm_MemMove( cbeg, cp, fclen);
  141.         }
  142.     else fclen= 0;
  143.     //cbeg[fclen]= 0;
  144.     
  145.     dataRemaining= fclen;    
  146.     return true;
  147. }
  148.  
  149.  
  150. DRichprocess* DRichHandler::NewProcessor( DFile* itsFile, Nlm_MonitorPtr progress)
  151. {
  152.         fProcessor = new DRichprocess( itsFile, fDoc, progress);
  153.         return fProcessor;
  154. }
  155.  
  156. void DRichHandler::InstallInStyle( DRichView* theDoc, DRichStyle* theStyle)
  157. {
  158.     if (fProcessor) {
  159.         // dummy proc for subclassing
  160.         }  
  161. }
  162.  
  163.  
  164. Boolean DRichHandler::ProcessFile( char* filename) 
  165. {
  166.     DFile afile(filename, "r");
  167.     return ProcessFile( &afile);
  168. }
  169.  
  170.  
  171. Boolean DRichHandler::ProcessFile( DFile* theFile) 
  172. {
  173.     Boolean result= false;
  174.     long startat= kNoData;
  175.     if (theFile) startat= IsRichFile(theFile);
  176.     if (fDoc && startat > kNoData) {
  177.         fDoc->SetAutoAdjust(false);  
  178.         
  179.         ulong nbytes; 
  180.         theFile->GetDataLength(nbytes);
  181.         if (nbytes) {
  182.             Nlm_MonitorPtr progress= NULL;
  183.             char prompt[128];
  184.             sprintf( prompt, "Converting %s format...", fFormatName);
  185.             progress= Nlm_MonitorIntNew( prompt, 0, nbytes);
  186.             theFile->Open();
  187.             theFile->Seek(startat);
  188.         
  189.             fProcessor= this->NewProcessor(theFile, progress);
  190.             if ( NULL != fProcessor) {
  191.                 fProcessor->Read();
  192.                 if (fProcessor->GetTitle()) 
  193.                     fDoc->GetWindow()->SetTitle( (char*)fProcessor->GetTitle());
  194.                 delete fProcessor; 
  195.                 fProcessor= NULL;
  196.                 result= true;
  197.                 }
  198.             
  199.             theFile->Close();
  200.             Nlm_MonitorFree(progress);
  201.             }        
  202.         fProcessStage= kAtEnd;
  203.         
  204.         fDoc->Select();
  205.         fDoc->Invalidate();
  206.         fDoc->AdjustScroll();
  207.         fDoc->SetAutoAdjust(true);  
  208.         }
  209.     return result;
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.